package pl.makimo.fuzzy.sets;

import pl.makimo.data.Record;
import pl.makimo.fuzzy.norms.Norm;

/* Chameleon class - works with all types of fuzzy sets, therefore it's not really clean */

public class CompoundFuzzySet<T> extends FuzzySet<T> {
	public FuzzySet<T> set1;
	public FuzzySet<T> set2;
	
	public CrispSet<T> elements = new CrispSet<T>();
	
	public Norm norm;
	
	public CompoundFuzzySet(FuzzySet<T> set1, FuzzySet<T> set2, Norm norm) {
		super();
		this.set1 = set1;
		this.set2 = set2;
		this.norm = norm;
		
		if (set1 instanceof DiscreteFuzzySet) {
			this.elements = ((DiscreteFuzzySet) set1).elements;
		} else if (set2 instanceof DiscreteFuzzySet) {
			this.elements = ((DiscreteFuzzySet) set2).elements;
		} else if (set1 instanceof CompoundFuzzySet) {
			this.elements = ((CompoundFuzzySet) set1).elements;
		} else if (set2 instanceof CompoundFuzzySet) {
			this.elements = ((CompoundFuzzySet) set2).elements;
		}
	}
	
	@Override
	public Object value(T element) {
		return (Double)norm.value(set1.value(element), set2.value(element));
	}

	/* Discrete implementations */
    public CrispSet<T> support() {    	    	
    	CrispSet<T> sup = new CrispSet<T>();
    	
    	for (T element : this.elements) {
    		if ((Double)this.value(element) > 0) {
    			sup.add(element);
    		}
    	}
    	
    	return sup;
    }
    
    public double in() {
    	return (double)support().cardinality() / crispCardinality();
    }
    
    public double crispCardinality() {
    	return this.elements.cardinality();
    }
    
    public double cardinality() {
    	return sigmaCount();
    }

    public double sigmaCount() {
    	double card = 0.0;
    	
    	for (T element : this.elements) {
    		card += (Double)this.value(element);
    	}
    	
    	return card;    	
    }

	@Override
	public double maxWithOne() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public double maxMembership() {
		// TODO Auto-generated method stub
		return 0;
	}
}
